package me.zhengjie.modules.housetype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.housetype.domain.HouseType;
import me.zhengjie.modules.housetype.service.HouseTypeService;
import me.zhengjie.modules.housetype.service.dto.HouseTypeDto;
import me.zhengjie.modules.housetype.service.dto.HouseTypeQueryParam;
import me.zhengjie.modules.housetype.service.mapper.HouseTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2022-01-29
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = HouseTypeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HouseTypeServiceImpl extends CommonServiceImpl<HouseTypeMapper, HouseType> implements HouseTypeService {

    // private final RedisUtils redisUtils;
    private final HouseTypeMapper houseTypeMapper;

    @Override
    public PageInfo<HouseTypeDto> queryAll(HouseTypeQueryParam query, Pageable pageable) {
        IPage<HouseType> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<HouseType> page = houseTypeMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, HouseTypeDto.class);
    }

    @Override
    public List<HouseTypeDto> queryAll(HouseTypeQueryParam query){
        return ConvertUtil.convertList(houseTypeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), HouseTypeDto.class);
    }

    @Override
    public HouseType getById(Long id) {
        return houseTypeMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public HouseTypeDto findById(Long id) {
        return ConvertUtil.convert(getById(id), HouseTypeDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(HouseTypeDto resources) {
        HouseType entity = ConvertUtil.convert(resources, HouseType.class);
        return houseTypeMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(HouseTypeDto resources){
        HouseType entity = ConvertUtil.convert(resources, HouseType.class);
        int ret = houseTypeMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return houseTypeMapper.deleteBatchIds(ids);
    }

    @Override
    public List<HouseType> getHouseType(Long id) {
        List<HouseType> houseTypeList = houseTypeMapper.selectList(new QueryWrapper<HouseType>().eq("real_estate_project_id", id));
        for (HouseType houseType : houseTypeList) {
            String houseTypeUrl = houseType.getHouseTypeUrl();
            houseTypeUrl = "http://101.201.82.10:8001/file/%E5%9B%BE%E7%89%87/"+houseTypeUrl;
            houseType.setHouseTypeUrl(houseTypeUrl);
        }
        return houseTypeList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<HouseTypeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (HouseTypeDto houseType : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("关联房产项目", houseType.getRealEstateProjectId());
              map.put("户型名称", houseType.getHouseTypeName());
              map.put("户型图片url", houseType.getHouseTypeUrl());
              map.put("户型价格", houseType.getHouseTypePrice());
              map.put("户型价格单位", houseType.getHouseTypePriceUnit());
              map.put("户型类型", houseType.getHouseType());
              map.put("户型分布", houseType.getHouseTypeDistribution());
              map.put("户型解析", houseType.getHouseTypeAnalysis());
              map.put("户型分类（几室）", houseType.getHouseTypeCategory());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
